package com.g7.dsp.forprotocol.ui.integrated;

import java.awt.BorderLayout;
import java.awt.Canvas;

import javax.swing.JFrame;

import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Shell;

import com.chinaway.dsp.test.MessageSend;
import com.g7.dsp.forprotocol.contexts.ApplicationContext;

public class Window {
	private JFrame jframe;
	DisplayThread displayThread;
	static Canvas canvas = null;
	static Shell shell;
	static Browser browser = null;
	ApplicationContext app;

	public Window(JFrame mainFrame) {
		this.jframe = mainFrame;
		this.app = ApplicationContext.getApplicationContext();
		System.setProperty("sun.awt.xembedserver", "true");
		app.initUI(jframe);
		displayThread = app.getSwtBean().getDisplayThread()!=null?app.getSwtBean().getDisplayThread():new DisplayThread();
		if(!displayThread.isAlive()){
		displayThread.start();
		}
		canvas = new Canvas() {
			private static final long serialVersionUID = 1L;

			@Override
			public void addNotify() {
				super.addNotify();
				final Canvas canvas = this;
				displayThread.getDisplay().asyncExec(new Runnable() {
					public void run() {
						//此句是swing集成swt的重点
						shell = SWT_AWT.new_Shell(displayThread.getDisplay(), canvas);
						shell.setText("Slashdot");
						shell.setLayout(new FillLayout());
						System.out.println("===Canvas===="+Thread.currentThread().getName());
						new MessageSend(displayThread.getDisplay(), shell);
					}
				});
			}
		};
		app.getSwtBean().setCanvas(canvas);
		app.getSwtBean().setDisplayThread(displayThread);
		this.jframe.add(BorderLayout.CENTER, canvas);
		this.jframe.setSize(1200, 550);
	}
}


